package com.zhangtai.modules.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dto.CredentialInfoDto;
import com.zhangtai.modules.entity.credential.CredentialHangEntity;
import com.zhangtai.modules.entity.credential.CredentialInfoEntity;
import com.zhangtai.modules.service.credential.CredentialHangService;
import com.zhangtai.modules.service.credential.CredentialInfoService;
import com.zhangtai.modules.vo.CredentialMemberVo;
import com.zhangtai.modules.vo.CredentialInfoVo;
import com.zhangtai.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;

import static com.zhangtai.modules.constVo.CredentialConst.HANG_STATUS_CHECKED;
import static com.zhangtai.modules.constVo.CredentialConst.HANG_STAUTS_CHECKING;

@RestController
@Api(tags="自助资质管理")
@RequestMapping("/credential")
@Slf4j
public class CredentialMemberController {
    @Autowired
    private CredentialInfoService credentialInfoService;
    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private CredentialHangService credentialHangService;
    @Autowired
    private JobController jobController;
    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    @ApiOperation("员工资质列表")
    @PostMapping("/member/getCredentialList")
    public R<PageResult<CredentialInfoDto>> getCredentialList(@RequestBody @Valid CredentialMemberVo vo, HttpServletRequest request)throws Exception{
        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        Long oid = userInfo.getLong("id");
        PageResult<CredentialInfoDto> result = credentialInfoService.getCredentialList(oid,vo.getPageNum(),vo.getPageSize());
        List<CredentialInfoDto> list = result.getList();
        if(!CollectionUtils.isEmpty(list)){
            for(CredentialInfoDto dto:list){
                dto.setJobName(jobController.jobMap.get(dto.getJobId()));
                dto.setOrgName(jobController.orgMap.get(dto.getOrgId()));
                dto.setUnitName(jobController.orgMap.get(dto.getUnitId1()));
            }
        }
        return R.ok(result);
    }

    @ApiOperation("保存/修改员工资质信息")
    @PostMapping("/member/saveOrUpdateCredential")
    @Transactional
    public R saveOrUpdateCredential(@RequestBody @Valid CredentialInfoVo entity, HttpServletRequest request)throws Exception{
        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        Long oid = userInfo.getLong("id");
        CredentialInfoEntity infoEntity = BeanUtilsMabach.doToDto(entity,CredentialInfoEntity.class);
        infoEntity.setOid(oid);
        if(ObjectUtils.isEmpty(infoEntity.getId())){
//            Long unitId = userInfo.getLong("depId");
            infoEntity.setUnitId(entity.getCompanyId());
            infoEntity.setCreateTime(new Date());
            credentialInfoService.saveE(infoEntity);
        }else{
            infoEntity.setUnitId(entity.getCompanyId());
            credentialInfoService.updateByIdE(infoEntity);
        }
        return R.ok();
    }

    @ApiOperation("删除员工资质信息")
    @DeleteMapping("/member/deleteCredential")
    @Transactional
    public R deleteCredential(String id){
        if(StringUtils.isEmpty(id)){
            return R.error("资质id为空");
        }
        credentialInfoService.removeOneById(Long.parseLong(id));
        return R.ok();
    }

    @PostMapping("/member/saveOrDeleteHang")
    @ApiOperation("添加/解除挂证")
    @Transactional
    public R saveOrDeleteHang(String credentialId,Integer type){
        if(StringUtils.isEmpty(credentialId)){
            return R.error("资质id为空");
        }
        CredentialInfoEntity entity =  credentialInfoService.getByIdE(Long.parseLong(credentialId));
        if(ObjectUtils.isEmpty(entity)){
            return R.error("资质信息不存在");
        }
        entity.setHangStatus(type);
        if(HANG_STAUTS_CHECKING.equals(type)){
             if(HANG_STATUS_CHECKED.equals(entity.getHangStatus())){
                return R.error("当前资质已挂靠成功！");
            }
            saveCredentialHang(Long.parseLong(credentialId));
        }else{
            credentialHangService.unVaildHang(Long.parseLong(credentialId));
        }
        credentialInfoService.updateByIdE(entity);
        return R.ok();
    }

    private void saveCredentialHang(Long credentialId){
        CredentialHangEntity hangEntity = new CredentialHangEntity();
        hangEntity.setCredentialId(credentialId);
        hangEntity.setCreateTime(new Date());
        hangEntity.setId(snowflakeIdWorker.nextId());
        credentialHangService.saveE(hangEntity);
    }

}
